package com.example.netlib.netwrapper;

import android.content.Context;

import com.example.netlib.ActionUrl;
import com.example.netlib.BaseHttpResultBean;
import com.szlanyou.thirdsdk.retrofit.callback.IHttpResultHandler;
import com.szlanyou.thirdsdk.retrofit.client.HttpClient;
import com.szlanyou.thirdsdk.retrofit.convert.AbsResponseBodyHttpResultHandler;
import com.szlanyou.thirdsdk.retrofit.util.GsonUtil;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;

/**
 * @author 陈章
 * create at 2021/7/8 10:04
 * desc:保存HttpClient实例
 * 本地Url测试
 */
public class NetHttpClientHolder {
    private static NetHttpClientHolder instance;
    private final HttpClient httpClient;
    private final ActionUrl actionUrl;

    private NetHttpClientHolder() {
        httpClient = new HttpClient() {
            @Override
            public void onCustomSettings(OkHttpClient.Builder builder) {

            }

            @Override
            public AbsResponseBodyHttpResultHandler createDefaultResponseBodyHttpResultHandler(IHttpResultHandler httpResultHandler) {
                return new AbsResponseBodyHttpResultHandler(httpResultHandler) {
                    @Override
                    public void onReceiveJson(String requestPageFlag,String requestCode, String json, IHttpResultHandler httpResultHandler, Class httpResultHandlerClass) {
                        BaseHttpResultBean baseHttpResultBean = GsonUtil.gson.fromJson(json, BaseHttpResultBean.class);

                        //根据字节码判断该给httpResultHandler返回什么值，是最外层的object还是object中的data。
                        if (httpResultHandlerClass == BaseHttpResultBean.class) {
                            httpResultHandler.onSuccess(requestPageFlag,requestCode, baseHttpResultBean);
                        } else if (httpResultHandlerClass == String.class) {
                            if (baseHttpResultBean.getResult() == 0) {
                                httpResultHandler.onSuccess(requestPageFlag,requestCode, baseHttpResultBean.getData());
                            } else {
                                httpResultHandler.onFailure(requestPageFlag,requestCode, null, baseHttpResultBean.getMes());
                            }
                        }
                    }
                };
            }
        };

        actionUrl = new ActionUrl(false);
    }

    public static NetHttpClientHolder getInstance() {
        if (instance == null) {
            synchronized (NetHttpClientHolder.class) {
                if (instance == null) {
                    instance = new NetHttpClientHolder();
                }
            }
        }
        return instance;
    }

    public void init(Context mContext, boolean... openLog) {
        httpClient.init(mContext, openLog);
        httpClient.setBaseUrl("http://122.152.205.50:8080/");

     /*   //设置公共参数
        httpClient.addCommonParam("commonParams1Key","commonParams1Value");
        httpClient.addCommonParam("commonParams2Key",222222);
        httpClient.addCommonParam("commonParams3Key",444444);


        httpClient.addCommonHeader("commonHeader1Key","commonHeader1Value");
        httpClient.addCommonHeader("commonHeader2Key",5555555);
        httpClient.addCommonHeader("commonHeader3Key",6666666);*/
    }

    public HttpClient getHttpClient() {
        return httpClient;
    }

    public ActionUrl getActionUrl() {
        return actionUrl;
    }
}